    //
//  MainViewController.m
//  ASIRequestDemo
//
//  Created by pinpaicon on 13-3-12.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "MainViewController.h"
#import "MainTabBarController.h"
#define URL @"http://app.heitugs.com/user/l_no.aspx?";

@interface MainViewController ()
-(void) login:(id)sender;  
-(void) GetErr:(ASIHTTPRequest *)request;  
-(void) GetResult:(ASIHTTPRequest *)request;  

@end

@implementation MainViewController
@synthesize username,password;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        
    }
    return self;
}

-(void) login:(id)sender  
{  
    //表单提交前的验证  
    if (username.text == nil||password.text==nil ) {  
        [tooles MsgBox:@"用户名或密码不能为空！"];  
        return;  
    }  
    //隐藏键盘  
    [username resignFirstResponder];  
    [password resignFirstResponder];  
    
    [tooles showHUD:@"正在登录...."];
    NSString *urlstr = URL;  
    NSURL *myurl = [NSURL URLWithString:urlstr];  
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myurl];  
    //设置表单提交项  
    [request setPostValue:username.text forKey:@"username"];
    [request setPostValue:password.text forKey:@"password"];
    [request setDelegate:self];
    [request setDidFinishSelector:@selector(GetResult:)];  
    [request setDidFailSelector:@selector(GetErr:)];  
    [request startAsynchronous];  
    
    
}  

//获取请求结果  
- (void)GetResult:(ASIHTTPRequest *)request{        
    //接收字符串数据
    NSString *str = [request responseString];
    NSLog(@"%@",str);
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [user setObject:str forKey:@"1"];
    

    
    //接收二进制数据åß
//    NSData *data = [request responseData];
    if ([str rangeOfString:@"1"].location==NSNotFound) {
        [tooles MsgBox:@"登录失败"];
        [tooles removeHUD];

 
    }else{
        MainTabBarController * pictureAndTextViewController = [[MainTabBarController alloc] init];
        
//        [ self presentViewController:pictureAndTextViewController animated: YES completion:nil];
        [self.navigationController pushViewController:pictureAndTextViewController animated:NO];
        
        [tooles removeHUD]; 
    }
 }
//- (void)dealloc
//{
//    [MainViewController release];
//
//}

//连接错误调用这个函数
- (void) GetErr:(ASIHTTPRequest *)request{
    
    [tooles removeHUD];  
    
    [tooles MsgBox:@"网络错误,连接不到服务器"];  
}  

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.view.backgroundColor = [UIColor colorWithRed:106/255.0 green:221/255.0 blue:86/255.0 alpha:1.0];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenWidth-100)/2, 80, 100, 100)];
    [imageView setImage:[UIImage imageNamed:@"登录logo"]];
    [imageView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:imageView];

    UILabel *txt1 = [[UILabel alloc] initWithFrame:CGRectMake(30,200,70,30)];
    [txt1 setText:@"用户名"];  
    [txt1 setBackgroundColor:[UIColor clearColor]];  
    [self.view addSubview:txt1];  
    
    UILabel *txt2 = [[UILabel alloc] initWithFrame:CGRectMake(30,240,70,30)];
    [txt2 setText:@"密   码"];  
    [txt2 setBackgroundColor:[UIColor clearColor]];  
    [self.view addSubview:txt2];  
    
    username = [[UITextField alloc]initWithFrame:CGRectMake(100,200, 150, 30)];
    username.delegate = self;
    [username setBorderStyle:UITextBorderStyleRoundedRect];  
    [self.view addSubview:username];  
    
    password = [[UITextField alloc]initWithFrame:CGRectMake(100,240, 150, 30)];
    password.delegate = self;
    [password setBorderStyle:UITextBorderStyleRoundedRect];  
    [password setSecureTextEntry:YES];  
    [self.view addSubview:password];  
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    [btn setTitle:@"登录" forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];  
    [btn setFrame:CGRectMake((kScreenWidth-190)/2, 280, 190, 40)];
    [btn setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:btn];  
    
    UILabel *banqianlabel = [[UILabel alloc]initWithFrame:CGRectMake((kScreenWidth-230)/2,kScreenHeight-40-30, 230, 40)];
    banqianlabel.text = @"哈尔滨德鹤农牧科技发展有限公司";
    banqianlabel.font = [UIFont systemFontOfSize:15];
    [banqianlabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:banqianlabel];
}

//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 32 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [username resignFirstResponder];
    [password resignFirstResponder];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

@end


